package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.antimoney.util.AlarmRule;
import com.turing.post.antimoney.util.AlarmRuleDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "antimoney.impl.AlarmRuleDaoImpl")
@Remote
@Local
public class AlarmRuleDaoImpl implements AlarmRuleDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 插入预警规则记录
	 */
	public void insert(AlarmRule ar) throws PCPSApplicationException {
		GeneralDao.insert(_em, ar);
	}

	@Override
	public void update(AlarmRule ar) throws PCPSApplicationException {
		GeneralDao.update(_em, ar);
	}

	@Override
	public AlarmRule getAlarmRuleById(BigInteger id) {
		return _em.find(AlarmRule.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AlarmRule> getAllAlarmRuleByIssueCompanyNo(String issueCompanyNo) {
		Query query = _em
				.createNativeQuery(
						"select t.* from AlarmRule t where t.issueCompanyNo =?1 and t.Status = '1'",
						AlarmRule.class);
		query.setParameter(1, issueCompanyNo);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AlarmRule> getAlarmRuleByCondition(PCPSWebInput webInput,
			String alarmruletype, String status, String alarmRuleDesc,
			Integer pageNo) {
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		if("".equals(issueCompanyNo)){
			return null;
		}
		String sql = "select t.* from AlarmRule t where t.issueCompanyNo ='"
				+ issueCompanyNo + "'";
		if (pageNo == -1) {
			sql = "select t.* from AlarmRule t where t.issueCompanyNo ='"
					+ issueCompanyNo + "'";
		} else {
			if (!PCPSUtil.isEmpty(alarmruletype))
				sql += " and t.alarmruletype='" + alarmruletype + "'";
			if (!PCPSUtil.isEmpty(status))
				sql += " and t.status='" + status + "'";
			if (!PCPSUtil.isEmpty(alarmRuleDesc))
				sql += " and t.alarmruledesc like '%" + alarmRuleDesc + "%'";
			sql += " order by t.alarmruleid";
		}
		Query query = _em.createNativeQuery(sql, AlarmRule.class);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * pageNo;
		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public Integer getAlarmRuleCountByCondition(PCPSWebInput webInput,
			String alarmruletype, String status, String alarmRuleDesc) {
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		if("".equals(issueCompanyNo)){
			return 0;
		}
		String sql = "select nvl(count(*),0) from AlarmRule t where t.issueCompanyNo ='"
				+ issueCompanyNo + "'";
		if (!PCPSUtil.isEmpty(alarmruletype))
			sql += " and t.alarmruletype='" + alarmruletype + "'";
		if (!PCPSUtil.isEmpty(status))
			sql += " and t.status='" + status + "'";
		if (!PCPSUtil.isEmpty(alarmRuleDesc))
			sql += " and t.alarmruledesc like '%" + alarmRuleDesc + "%'";
		Query query = _em.createNativeQuery(sql);
		return Integer.parseInt(query.getSingleResult().toString());
	}

	@Override
	public BigInteger getMaxOperId() {
		Query query = _em
				.createNativeQuery("select nvl(max(alarmruleid),0)+1 alarmruleid from AlarmRule t");
		return new BigInteger(query.getSingleResult().toString());
	}
}